package com.by.zhdoctor.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.by.zhdoctor.domain.Userinfo;

import com.by.zhdoctor.repository.UserinfoRepository;
import com.by.zhdoctor.web.rest.util.HeaderUtil;
import com.by.zhdoctor.web.rest.util.PaginationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing Userinfo.
 */
@RestController
@RequestMapping("/api")
public class UserinfoResource {

    private final Logger log = LoggerFactory.getLogger(UserinfoResource.class);

    @Inject
    private UserinfoRepository userinfoRepository;

    /**
     * POST  /userinfos : Create a new userinfo.
     *
     * @param userinfo the userinfo to create
     * @return the ResponseEntity with status 201 (Created) and with body the new userinfo, or with status 400 (Bad Request) if the userinfo has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/userinfos")
    @Timed
    public ResponseEntity<Userinfo> createUserinfo(@RequestBody Userinfo userinfo) throws URISyntaxException {
        log.debug("REST request to save Userinfo : {}", userinfo);
        if (userinfo.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("userinfo", "idexists", "A new userinfo cannot already have an ID")).body(null);
        }
        Userinfo result = userinfoRepository.save(userinfo);
        return ResponseEntity.created(new URI("/api/userinfos/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert("userinfo", result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /userinfos : Updates an existing userinfo.
     *
     * @param userinfo the userinfo to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated userinfo,
     * or with status 400 (Bad Request) if the userinfo is not valid,
     * or with status 500 (Internal Server Error) if the userinfo couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/userinfos")
    @Timed
    public ResponseEntity<Userinfo> updateUserinfo(@RequestBody Userinfo userinfo) throws URISyntaxException {
        log.debug("REST request to update Userinfo : {}", userinfo);
        if (userinfo.getId() == null) {
            return createUserinfo(userinfo);
        }
        Userinfo result = userinfoRepository.save(userinfo);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert("userinfo", userinfo.getId().toString()))
            .body(result);
    }

    /**
     * GET  /userinfos : get all the userinfos.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of userinfos in body
     * @throws URISyntaxException if there is an error to generate the pagination HTTP headers
     */
    @GetMapping("/userinfos")
    @Timed
    public ResponseEntity<List<Userinfo>> getAllUserinfos(Pageable pageable)
        throws URISyntaxException {
        log.debug("REST request to get a page of Userinfos");
        Page<Userinfo> page = userinfoRepository.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/userinfos");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /userinfos/:id : get the "id" userinfo.
     *
     * @param id the id of the userinfo to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the userinfo, or with status 404 (Not Found)
     */
    @GetMapping("/userinfos/{id}")
    @Timed
    public ResponseEntity<Userinfo> getUserinfo(@PathVariable Long id) {
        log.debug("REST request to get Userinfo : {}", id);
        Userinfo userinfo = userinfoRepository.findOne(id);
        return Optional.ofNullable(userinfo)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * GET  /userinfos/:id : get the "phonenumber" userinfo.
     *
     * @param phonenumber the id of the userinfo to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the userinfo, or with status 404 (Not Found)
     */
    @GetMapping("/userinfos-phonenumber/{phonenumber}")
    @Timed
    public ResponseEntity<Userinfo> getUserinfoByPhonenumber(@PathVariable String phonenumber) {
        log.debug("REST request to get Userinfo : {}", phonenumber);
        Userinfo userinfo = userinfoRepository.findOneByPhonenumber(phonenumber);
        return Optional.ofNullable(userinfo)
            .map(result -> new ResponseEntity<>(
                result,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }
    /**
     * DELETE  /userinfos/:id : delete the "id" userinfo.
     *
     * @param id the id of the userinfo to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/userinfos/{id}")
    @Timed
    public ResponseEntity<Void> deleteUserinfo(@PathVariable Long id) {
        log.debug("REST request to delete Userinfo : {}", id);
        userinfoRepository.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("userinfo", id.toString())).build();
    }

}
